﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Dir
{
    class CSS_Style
    {
        private enum ParseState
        {
            Null, St, Key
        }
        private ParseState state = ParseState.Null;
        private string path = null;

        public CSS_Style(string path)
        {
            this.path = path;
        }

        public void CreatefileCSS() // или добавить path2 , а также 3-й параметр для стиля
        {
            if (File.Exists(path))
            {
                try
                {
                    StreamReader sr = new StreamReader(path);
                    string source = sr.ReadToEnd();
                    sr.Close();
                    StringBuilder formatted = new StringBuilder();
                    StringBuilder word = new StringBuilder();
                    int i = 0;

                    while (i <= source.Length)
                    {
                        char ch = i < source.Length ? source[i] : (char)0,
                             prevCh = i > 0 ? source[i - 1] : (char)0,
                             nextCh = i < source.Length - 1 ? source[i + 1] : (char)0;
                        switch (state)
                        {
                            case ParseState.Null:
                                if (ch != (char)32 && (ch != '\r' && ch != '\n'))
                                {

                                    formatted.Append(ch);
                                    if (char.IsLetter(ch) || ch == '<' || ch == '>' || ch == '/')
                                    {
                                        word.Append(ch);
                                        string w = word.ToString();
                                        if (w == "</style><pre>")
                                        {
                                            formatted.Append("<div class=\"example\">\r\n<p> ");
                                            state = ParseState.St;
                                            word.Remove(0, word.Length);
                                            i += 2;
                                        }
                                        if (w == "</pre></body></html>")
                                        {
                                           // formatted.Remove(formatted.Length - w.Length, 1);
                                            formatted.Insert(formatted.Length - w.Length - 1, "</div>");
                                        }
                                    }

                                }
                                else
                                {
                                    if (ch == ' ' || ch == '\n')
                                        word.Remove(0, word.Length);
                                    formatted.Append(ch);
                                }
                                break;

                            case ParseState.St:
                                if (i == source.Length - 22)
                                {
                                    i--;
                                    state = ParseState.Null;
                                }
                                else
                                    if (prevCh == '\r' && ch == '\n')
                                    {
                                        //formatted.Remove(formatted.Length, 1);
                                        //formatted.Insert(i - 2, "</p>");
                                        formatted.Append(ch).Append("<p> ");
                                        formatted.Insert(formatted.Length - 6, "</p>");
                                    }
                                    else
                                        formatted.Append(ch);
                                    
                                break;
                            //ParseState.Key:

                            //break;
                            default:
                                break;
                        }
                        i++;
                    }
                    //return formatted.ToString();
                    //sr.Close();
                    StreamWriter sw = new StreamWriter(path, false); //false данные перезаписываютя
                    sw.WriteLine(formatted.ToString());
                   // sw.WriteLine("11111");

                    sw.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(e.Message);
                }

            }

        }

        public static void Parse(string path)
        {
            CSS_Style f = new CSS_Style(path);
            f.CreatefileCSS();
        }
    }
}
